import { loginAPI } from '@/api'
import { getToken, setToken, getRefreshToken, setRefreshToken } from '@/utils/token.js'
const state = {
  token: getToken() || '',
  refresh_token: getRefreshToken() || ''
}
const mutations = {
  SETTOKEN(state, token) {
    state.token = token
    setToken(token)
  },
  SETREFRESHTOKEN(state, refresh_token) {
    state.refresh_token = refresh_token
    setRefreshToken(refresh_token)
  }
}

const actions = {
  async asyncLoginAction(store, val) {
    try {
      const res = await loginAPI(val)
      const { token, refresh_token } = res.data.data

      store.commit('SETTOKEN', token)
      store.commit('SETREFRESHTOKEN', refresh_token)
      // 返回成功的promise对象
      return Promise.resolve(res)
    } catch (error) {
      // 返回失败的promise对象
      return Promise.reject(error)
    }
  }

}
const getters = {
}

export default {
  state,
  mutations,
  actions,
  getters
}
